//
//  MainTabBarController.m
//  BigToBig
//
//  Created by guochuangkeshi on 16/4/22.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "MainTabBarController.h"
#import "HomeController.h"
#import "LivingController.h"
#import "RenheViewController.h"
#import "PersonCenterController.h"
#import "LocationViewController.h"


@interface MainTabBarController ()
@property (nonatomic , strong)NSArray *array;

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupChildController];
    [self createData];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

- (void)setupChildController{
        
    HomeController *homeVc=[[HomeController alloc]init];
    homeVc.tabBarItem.title=@"天时";
    homeVc.tabBarItem.image=[UIImage imageNamed:@"矩形_6"];
    homeVc.tabBarItem.selectedImage=[[UIImage imageNamed:@"矩形-6"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *homeNav = [[UINavigationController alloc]initWithRootViewController:homeVc];
    
    
    
    LocationViewController *livingVc=[[LocationViewController alloc]init];
    livingVc.tabBarItem.title=@"地利";
    livingVc.tabBarItem.image=[UIImage imageNamed:@"团队合作_"];
    livingVc.tabBarItem.selectedImage=[[UIImage imageNamed:@"团队合作"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *livingNav=[[UINavigationController alloc]initWithRootViewController:livingVc];
    
    
    
    RenheViewController *renheVC = [[RenheViewController alloc] init];
    renheVC.tabBarItem.title = @"人和";
    renheVC.tabBarItem.image = [UIImage imageNamed:@"图层_41"];
    renheVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"图层-41"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *renheNav = [[UINavigationController alloc] initWithRootViewController:renheVC];
    
    
    
    PersonCenterController *personVc=[[PersonCenterController alloc]init];
    personVc.tabBarItem.title=@"个人中心";
    personVc.tabBarItem.image=[UIImage imageNamed:@"Compound_Shape"];
    personVc.tabBarItem.selectedImage=[[UIImage imageNamed:@"Compound-Shape"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *personNav=[[UINavigationController alloc]initWithRootViewController:personVc];
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
    [dict setObject:[UIColor lightGrayColor] forKey:NSForegroundColorAttributeName];
    
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
    
    [dict1 setObject:[UIFont systemFontOfSize:12] forKey:NSFontAttributeName];
    [dict1 setObject:COLOR_RGBA(88, 165, 255, 1.0) forKey:NSForegroundColorAttributeName];
    [[UITabBarItem appearance]setTitleTextAttributes:dict forState:UIControlStateNormal];
    [[UITabBarItem appearance]setTitleTextAttributes:dict1 forState:UIControlStateSelected];
    
    [[UINavigationBar appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName]];
    
    self.viewControllers=[NSArray arrayWithObjects:homeNav,livingNav,renheNav,personNav,nil];
    
    self.selectedViewController=homeNav;

}

- (void)createData{
    
    self.array = [PHKitTool getAllImageGroup];
    PHImageManager * imageManager = [[PHImageManager alloc]init];
    dispatch_queue_t queue =  dispatch_queue_create("iamgeQueue", NULL);
    
    PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
    option.synchronous = YES;
    for (NSInteger i = 0; i < _array.count; i++) {
        
        PHAsset *asset = _array[i];
        
        
        dispatch_async(queue, ^{
            //            NSLog(@"%@---%zd",[NSThread currentThread],index);
            
            [imageManager requestImageForAsset:asset targetSize:CGSizeMake((kScreenWidth - 6) / 4 , (kScreenWidth - 6) / 4) contentMode:1 options:option resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
                if (result != nil) {
                    //                     NSLog(@"loop index :%ld", i);
                    
                    [[MainTabBarController shareImageArray] addObject:result];
                }
            }];
            
        });
        
        
    }
    
    
}
+(NSMutableArray *)shareImageArray
{
    
    static NSMutableArray *imageArray = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        imageArray = [[NSMutableArray alloc] init];
    });
    
    return imageArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
